package com.library.controller;

import com.example.common.Result;
import com.library.entity.Book;
import com.library.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("/getAllBooks")
    public Result<List> getAllBooks() {
        return Result.success(bookService.getAllBooks());
    }

    @GetMapping("/getBookById/{id}")
    public Result<Book> getBookById(@PathVariable(name = "id") Long id) {
        return Result.success(bookService.getBookById(id));
    }

    @GetMapping("/isbn/{isbn}")
    public Result<Book> getBookByIsbn(@PathVariable(name = "isbn") String isbn) {
        return Result.success(bookService.getBookByIsbn(isbn));
    }

    @PostMapping("/createBook")
    public Result<Book> createBook(@RequestBody Book book) {
        return Result.success(bookService.createBook(book));
    }

    @PutMapping("/updateBook/{id}")
    public Result<Book> updateBook(@PathVariable(name = "id") Long id, @RequestBody Book book) {
        return Result.success(bookService.updateBook(id, book));
    }

    @DeleteMapping("deleteBook/{id}")
    public Result<String> deleteBook(@PathVariable(name = "id") Long id) {
        bookService.deleteBook(id);
        return Result.success();
    }
}